﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using School.Entities;
using School.Common;
using System.Data.Entity;
using School.Admission;
using System.Web.UI.HtmlControls;

namespace School.Pages.Admission
{
    public partial class StudentDetailsSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                ((HtmlForm)this.Master.FindControl("form1")).Target = "_blank";
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1016) == true)
                {

                    if (!(Page.IsPostBack))
                    {
                        Fillcrls();

                        InitializeControls();

                        chName.Checked = true;
                        chName_CheckedChanged(null, null);
                        //GridDataBind();

                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        # region "Collect Data"

        //***************** This is function to bind data grid  ***************************
        //-**************************************************************************
        protected void GridDataBind()
        {
            Int32 mycount;
            Hashtable SearchTable = new Hashtable();

            if (txtSearchFileNumber.Text.Length > 0)
            {
                SearchTable.Add("FileNumber", txtSearchFileNumber.Text.ToString());
            }
            if (ddlSearchcampus_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Campus_Id", ddlSearchcampus_Id.SelectedValue.ToString());
            }

            if (ddlSearchCertificate_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Certificate_Id", ddlSearchCertificate_Id.SelectedValue.ToString());
            }
            if (ddlSearchStage_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Stage_Id", ddlSearchStage_Id.SelectedValue.ToString());
            }
            if (ddlSearchGrade_ID.SelectedIndex > 0)
            {
                SearchTable.Add("Grade_ID", ddlSearchGrade_ID.SelectedValue.ToString());
            }
                        
            if (txtSearchRegistrationNumber.Text.Length > 0)
            {
                SearchTable.Add("RegistrationNumber", txtSearchRegistrationNumber.Text.ToString());
            }                                                            

            long campus = 0;
            long certificate = 0;
            long stage = 0;
            long grade = 0;
            string name = txtSearchName.Text;

            long.TryParse(ddlSearchcampus_Id.SelectedValue, out campus);
            long.TryParse(ddlSearchCertificate_Id.SelectedValue, out certificate);
            long.TryParse(ddlSearchStage_Id.SelectedValue, out stage);
            long.TryParse(ddlSearchGrade_ID.SelectedValue, out grade);

            var AllStudentsData = Students.SearchDetailedFiltered(campus, certificate, stage, grade, name);
            griddata.DataSource = AllStudentsData;//AllStudentsData;

            griddata.DataBind();
            griddata.Visible = true;
            mycount = AllStudentsData.Count();
            lblRecordNo.Text = "عدد نتائج البحث = " + mycount.ToString();
            if (mycount == 0)
            {
                lblMessage.Text = "لا يوجد بيانات";
            }
            else
            {
                lblMessage.Text = "";
            }

        }

        # endregion


        # region "Fill all crls"

        //***************** This is function to fill crl  ***************************
        //-**************************************************************************

        protected void Fillcrls()
        {
            ddlSearchcampus_Id.DataSource = Structure.GetCampuses();
            ddlSearchcampus_Id.DataTextField = "Name";
            ddlSearchcampus_Id.DataValueField = "Id";
            ddlSearchcampus_Id.DataBind();
            ddlSearchcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));           

        }


        //***************** This is function to initlaize crl  ***************************
        //-**************************************************************************
        protected void InitializeControls()
        {
            txtSearchFileNumber.Text = "";
            txtSearchRegistrationNumber.Text = "";
        }       
        # endregion        

        protected void ddlSearchcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlSearchCertificate_Id.Items.Clear();
            ddlSearchGrade_ID.Items.Clear();
            ddlSearchStage_Id.Items.Clear();
            ddlSearchCertificate_Id.DataSource = Structure.GetCertificatesByCampus(Convert.ToInt64(ddlSearchcampus_Id.SelectedValue));
            ddlSearchCertificate_Id.DataTextField = "Name";
            ddlSearchCertificate_Id.DataValueField = "Id";
            ddlSearchCertificate_Id.DataBind();
            ddlSearchCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
            ddlSearchCertificate_Id.Enabled = true;
            ddlSearchGrade_ID.Enabled = false;
            ddlSearchStage_Id.Enabled = false;
        }

        protected void ddlSearchCertificate_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSearchCertificate_Id.SelectedIndex > 0)
            {
                ddlSearchStage_Id.Items.Clear();
                ddlSearchGrade_ID.Items.Clear();
                ddlSearchStage_Id.DataSource = Structure.GetStagesByCertificate(Convert.ToInt64(ddlSearchCertificate_Id.SelectedValue));
                ddlSearchStage_Id.DataTextField = "Name";
                ddlSearchStage_Id.DataValueField = "id";
                ddlSearchStage_Id.DataBind();
                ddlSearchStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlSearchStage_Id.Enabled = true;
                ddlSearchGrade_ID.Enabled = false;
            }
        }

        protected void ddlSearchStage_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSearchStage_Id.SelectedIndex > 0)
            {

                ddlSearchGrade_ID.Items.Clear();
                ddlSearchGrade_ID.DataSource = Structure.GetGradesByStage(Convert.ToInt64(ddlSearchStage_Id.SelectedValue));
                ddlSearchGrade_ID.DataTextField = "Name";
                ddlSearchGrade_ID.DataValueField = "id";
                ddlSearchGrade_ID.DataBind();
                ddlSearchGrade_ID.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlSearchGrade_ID.Enabled = true;
            }
        }

        protected void griddata_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlbrows.Visible = false;
            pnldetails.Visible = true;
            DetailsView1bind();
        }

        protected void griddata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griddata.PageIndex = e.NewPageIndex;
            GridDataBind();
        }

        protected void griddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                griddata.SelectedIndex = e.RowIndex;

                long CID = Int32.Parse(griddata.SelectedDataKey.Value.ToString());
                Boolean successadd = FileOrder.Delete(CID);
                if (successadd == true)
                {
                    GridDataBind();
                    lblMessage.Visible = true;
                    lblMessage.Text = "Record(s) was deleted successfully";

                }
            }
            catch (Exception)
            {
                lblMessage.Text = "Record(s) cannot be deleted, please contact your administrator";

            }
        }

        protected void griddata_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton LB = (LinkButton)e.Row.FindControl("LinkButton1");
                LB.Attributes.Add("onclick", "javascript:return " +
                "confirm('Are you sure you want to delete record(s) ( " +
                DataBinder.Eval(e.Row.DataItem, "Id") + " ) ?')");

            }
        }

        protected void DetailsView1bind()
        {
            Hashtable SearchTable = new Hashtable();

            SearchTable.Add("Id", griddata.SelectedDataKey.Value.ToString());
            DetViewdata.DataSource = Student.All(SearchTable);
            DetViewdata.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridDataBind();
        }

        protected void btnGetAll_Click(object sender, EventArgs e)
        {
            TemplateField asd = new TemplateField();
            asd.HeaderText = "asd";

            InitializeControls();
            GridDataBind();
        }

        protected void chFileNumber_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[0].Visible = chFileNumber.Checked;
        }

        protected void chRegistrationNumber_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[1].Visible = chRegistrationNumber.Checked;
        }

        protected void chName_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[2].Visible = chName.Checked;
        }

        protected void chBirthDate_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[3].Visible = chBirthDate.Checked;
        }

        protected void chRelign_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[4].Visible = chRelign.Checked;
        }
        

        protected void chNationalId_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[5].Visible = chNationalId.Checked;
        }

        protected void chPassport_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[6].Visible = chPassport.Checked;
        }

        protected void chGender_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[7].Visible = chGender.Checked;
        }

        //protected void chCertificate_CheckedChanged(object sender, EventArgs e)
        //{
        //    griddata.Columns[8].Visible = chCertificate.Checked;
        //}
        

        protected void chFatherQualification_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[9].Visible = chFatherQualification.Checked;
        }

        protected void chFatherSpecialization_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[10].Visible = chFatherSpecialization.Checked;
        }

        protected void chFatherPointOfGraduation_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[11].Visible = chFatherPointOfGraduation.Checked;
        }

        protected void chFatherJob_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[12].Visible = chFatherJob.Checked;
        }

        protected void chMotherQualification_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[13].Visible = chMotherQualification.Checked;
        }

        protected void chMotherSpecialization_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[14].Visible = chMotherSpecialization.Checked;
        }

        protected void chMotherPointOfGraduation_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[15].Visible = chMotherPointOfGraduation.Checked;
        }

        protected void chMotherJob_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[16].Visible = chMotherJob.Checked;
        }

        protected void chStudentDat_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[17].Visible = chStudentDat.Checked;
        }

        protected void chOthers_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[18].Visible = chOthers.Checked;
        }

        protected void chDelete_CheckedChanged(object sender, EventArgs e)
        {
            griddata.Columns[19].Visible = chDelete.Checked;
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            Session["stcampus"] = Session["stcertificate"] = Session["ststage"] = Session["stgrade"] = string.Empty;
            Session["stname"] = txtSearchName.Text; 
            Session["stFileNumber"] = txtSearchFileNumber.Text;
            Session["stReistrationNumber"] = txtSearchRegistrationNumber.Text;
            if (ddlSearchcampus_Id.SelectedIndex >0)
                Session["stcampus"] = ddlSearchcampus_Id.SelectedValue;
            if (ddlSearchCertificate_Id.SelectedIndex > 0)
                Session["stcertificate"] = ddlSearchCertificate_Id.SelectedValue;
            if (ddlSearchStage_Id.SelectedIndex > 0)
                Session["ststage"] = ddlSearchStage_Id.SelectedValue;
            if (ddlSearchGrade_ID.SelectedIndex > 0)
                Session["stgrade"] = ddlSearchGrade_ID.SelectedValue;          

            ///////////////////////////////////////////////////
            bool[] visibles = new bool[17];
            visibles[0] = chName.Checked;
            visibles[1] = chRegistrationNumber.Checked;
            visibles[2] = chBirthDate.Checked;
            visibles[3] = chNationalId.Checked;
            visibles[4] = chPassport.Checked;
            visibles[5] = chGender.Checked;
            visibles[6] = chFatherJob.Checked;
            visibles[7] = chFatherSpecialization.Checked;
            visibles[8] = chFatherQualification.Checked;
            visibles[9]  = chMotherJob.Checked;
            visibles[10] = chMotherQualification.Checked;
            visibles[11] = chRelign.Checked;
            visibles[12] = chFileNumber.Checked;
            visibles[13] = chMotherPointOfGraduation.Checked;
            visibles[14] = chMotherSpecialization.Checked;
            visibles[15] = chFatherPointOfGraduation.Checked;
          
            

 
            Session["visibles"] = visibles;
            
            Response.Redirect("/Admission/Reports/showstudentsDetailsReport.aspx");
        }                                                                                         
          
    }
}